package cn.signalpha.modules.shop.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.dto.CartDTO;
import cn.signalpha.modules.shop.entity.Cart;
import cn.signalpha.modules.shop.service.CartService;
import cn.signalpha.modules.shop.vo.CartVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 购物车
 *
 * @author suguangjun
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/wap/cart")
@Api(value = "购物车", tags = "购物车")
public class ApiCartController extends BaseController {

    private final CartService cartService;

	@GetMapping("/count")
	@ApiOperationSupport(order = 1)
	@ApiOperation("购物车商品数量")
	public R count(@ApiIgnore @RequestParam Map<String, Object> params){
		return R.data(cartService.count(Wrappers.<Cart>lambdaQuery().eq(Cart::getUserId, getUser().getUserId())));
	}

    @GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "type", value = "类型：1c端 2b端", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 2)
    @ApiOperation("获取购物车商品列表")
    public R<List<CartVO>> list(@ApiIgnore @RequestParam Map<String, Object> params){
		params.put("userId", getUser().getUserId().toString());
        return R.data(cartService.getShopCarts(cartService.getSellerCartItemList(params)));
    }

    @PostMapping("/save")
	@ApiOperationSupport(order = 3)
    @ApiOperation(value = "添加商品到购物车",notes = "接口返回购物车id")
    public R save(@Valid @RequestBody CartDTO.SaveDTO saveDTO){
        return R.data(cartService.addGoodsToCart(saveDTO, getUser().getUserId()));
    }

    @PostMapping("/update")
	@ApiOperationSupport(order = 4)
    @ApiOperation(value = "修改购物车商品",notes = "传id和goodsNumber")
    public R update(@RequestBody CartDTO.UpdateDTO updateDTO){
        return R.status(cartService.updateById(Cart.builder().id(updateDTO.getId()).goodsNum(updateDTO.getGoodsNumber()).build()));
    }

    @PostMapping("/remove")
	@ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除", notes = "批量删除逗号分割")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids){
        return R.status(cartService.removeByIds(Func.toLongList(ids)));
    }

}
